//
//  ACFileManagerViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/1.
//

#import "ACFileManagerViewController.h"
#import "ACFileListCell.h"
#import "ACFileInfoViewController.h"



@interface ACFileManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray<ACFileModel *>*dataArray;

@end

@implementation ACFileManagerViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 109;
        _tableview.backgroundColor = UIColorFromRGB(0xF4F5F8);
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACFileListCell" bundle:curB] forCellReuseIdentifier:@"ACFileListCell"];
        _tableview.tableFooterView = [UIView new];
    }
    return _tableview;
}
-(NSMutableArray<ACFileModel *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"文件列表";
    label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    label.textColor = UIColorFromRGB(0x30322C);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_offset(16);
    }];
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"live_live_close_gray" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(46, 46));
        make.centerY.equalTo(label.mas_centerY);
    }];
    @weakify(self)
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-ACSafeAreaBottomHeight);
        make.top.equalTo(label.mas_bottom).offset(28);
    }];
}
-(void)setLiveID:(NSString *)liveID{
    _liveID = liveID;
    [self requestData];
}
-(void)requestData{
    if (self.index == 1 || self.index == 2) { // 直播间文件列表
        [SVProgressHUD show];
        [ACLiveRoomManager requestFileListWithLiveID:self.liveID success:^(id  _Nonnull responseObject) {
            [SVProgressHUD dismiss];
            NSArray *dic = [responseObject valueForKey:@"materials"];
            self.dataArray = [ACFileModel mj_objectArrayWithKeyValuesArray:dic];
            [self.tableview reloadData];
        } failure:^(NSError * _Nonnull error) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:@"网络错误"];
        }];
    }else{
        
    }
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACFileListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACFileListCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACFileListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACFileListCell"];
    }
    [cell setBtnStyle:self.index];
    ACFileModel *fileModel = self.dataArray[indexPath.row];
    cell.fileModel = fileModel;
    @weakify(self)
    cell.clickActionHandler = ^(BOOL isSelected) {
        @strongify(self)
        if (self.fileManagerSwitchhandler) {
            self.fileManagerSwitchhandler(fileModel);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ACFileModel *fileModel = self.dataArray[indexPath.row];
    ACFileInfoViewController *fileInfo = [[ACFileInfoViewController alloc] init];
    fileInfo.id_file = fileModel.id_file;
}


@end
